#include "ZException.h"
#include "../Tools/ZLogManager.h"

GLOBAL_NAMESPACE_BEGIN

Exception::Exception(int num, const std::string& desc, const std::string& src)
    : line(0),
    number(num),
    description(desc),
    source(src)
{
}

Exception::Exception(int num, const std::string& desc, const std::string& src,
    const char* typ, const char* fil, long lin)
    : line(0),
    number(num),
    description(desc),
    source(src),
    typeName(typ),
    file(fil)
{
    // Log this error, mask it from debug though since it may be caught and ignored
    if (LogManager::getSingletonPtr())
    {
        LogManager::getSingleton().logMessage(this->getFullDescription(), LOGLEVEL_CRITICAL, true);
    }
}

Exception::Exception(const Exception& rhs)
    : line(rhs.line),
    number(rhs.number),
    description(rhs.description),
    source(rhs.source),
    typeName(rhs.typeName),
    file(rhs.file)
{}

void Exception::operator=(const Exception& rhs)
{
    line        = rhs.line;
    number      = rhs.number;
    description = rhs.description;
    source      = rhs.source;
    typeName    = rhs.typeName;
    file        = rhs.file;
}

int Exception::getNumber() const throw()
{
    return number;
}

const std::string& Exception::getFullDescription() const
{
    if (fullDesc.empty())
    {
        std::stringstream ss;
        ss << "ZFRAMEWORK EXCEPTION(" << number << ":" << typeName << "): "
            << description << " in " << source;
        if (line > 0)
        {
            ss << " at " << file << " (line " << line << ")";
        }
        fullDesc = ss.str();
    }
    return fullDesc;
}

GLOBAL_NAMESPACE_END